package listbyorder.access101_200.test189;

/**
 * @author code_yc
 * @version 1.0
 * @date 2020/6/15 13:13
 */
public class Solution4 {

    // 每次搬运的时候，设置一个值作为缓存
    public void rotate(int[] nums, int k) {
        int n = nums.length;
        k %= n;
        if (k == 0) return;
        int count = 0;
        int start = 0;
        int current = start;
        int pre = nums[current];
        while (true) {
            do {
                int next = (current + k) % n;
                int temp = nums[next];
                nums[next] = pre;
                pre = temp;
                current = next;
                count++;
                if (count == n) {
                    return;
                }
            } while (start != current);
            start++;
            current = start;
            pre = nums[current];
        }
    }
}
